package com.wx.web.controller.bankCard;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.wx.business.course.WtBankCardService;
import com.wx.business.trade.IxbUserService;
import com.wx.business.util.BankCardUtil;
import com.wx.dao.mapper.WtBankCardMapper;
import com.wx.model.IxbUser;
import com.wx.model.WtBankCard;
import com.wx.model.WtBankCardExample;

/**
 * 银行卡控制器
 *
 * @author wemo
 * @Date 2018-08-08 18:27:50
 */
@Controller
@RequestMapping("/card")
public class BankCardController {
	
	 @Autowired
     public  WtBankCardService  wtBankCardService;
	 
	 @Autowired
     public  WtBankCardMapper  wtBankCardMapper;
	 
	 @Autowired
     public  IxbUserService  userService;
     /**
      * 银行卡列表
      * @param sid
      * @return
      */
    @RequestMapping(value = "/list")
    @ResponseBody
    public JsonResult cardList(HttpServletRequest request ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		WtBankCardExample ext=new WtBankCardExample();
		ext.createCriteria().andUserIdEqualTo(user.getSid());
		List<WtBankCard> cardList=wtBankCardService.selectByExample(ext);
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("cardList", cardList);
		return new JsonResult(true,result);
    }
    
    /**
     * 添加银行卡
     * @param sid
     * @return
     */
   @RequestMapping(value = "/add")
   @ResponseBody
   public JsonResult addBankCard(HttpServletRequest request,WtBankCard card ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		
		WtBankCardExample ext=new WtBankCardExample();
		ext.createCriteria().andCardNoEqualTo(card.getCardNo());
		List<WtBankCard> cardList=wtBankCardService.selectByExample(ext);
		
		if(!cardList.isEmpty())
		{
			return new JsonResult(false,"银行卡已经被绑定过");
		}
		
		 WtBankCard onlineCard=	BankCardUtil.getBasicBankCard(card.getCardNo());
		 if(onlineCard==null)
		 {
			 return new JsonResult(false,"银行卡信息不正确");
		 }
		onlineCard.setCreateTime(new Date());
		onlineCard.setUserId(user.getSid());
		onlineCard.setUserRealName(card.getUserRealName());
		onlineCard.setCardNo(card.getCardNo());
		wtBankCardService.insertSelective(onlineCard);
    
		return new JsonResult(true,"添加成功");
   }
   
   
   
   /**
    * 删除银行卡
    * @param sid
    * @return
    */
  @RequestMapping(value = "/delete")
  @ResponseBody
  public JsonResult delete(HttpServletRequest request,String id ) {
		wtBankCardService.deleteByPrimaryKey(id);
   
		return new JsonResult(true,"删除成功");
  }
  
  /**
   * 
   * @param session
   * @param password
   * @return  
   * @Description:银行卡基本信息
   */
  @RequestMapping(value = "/getBasicCardMsg", method = { RequestMethod.POST })
  @ResponseBody
  public JsonResult myBankCards(HttpServletRequest request,String cardNo)
  {
   
     WtBankCard card=	BankCardUtil.getBasicBankCard(cardNo);
     
     if(card==null){
  	   return new JsonResult(false, "银行卡信息不正确");
     }
     
     Map<String, Object> map = new HashMap<String, Object>();
     map.put("bankCard", card);
     return new JsonResult(true, map);
     
  }
  
  /**
   * 提现
   * @param session
   * @param password
   * @return  
   * @Description:提现
   */
  @RequestMapping(value = "/getCardMsg", method = { RequestMethod.POST })
  @ResponseBody
  public JsonResult withdrawMoney(HttpServletRequest request,int cardId)
  {
	  
	  WtBankCard card=	wtBankCardMapper.selectByPrimaryKey(cardId);
	  IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
	  IxbUser dbuser=userService.selectByPrimaryKey(user.getSid());
	  Map<String, Object> map = new HashMap<String, Object>();
	  map.put("bankCard", card);
	  map.put("userBalance", dbuser.getUserBalance());
	  return new JsonResult(true, map);
	  
  }
  
   
}
